<?php

/**
 * This is the model class for table "tbl_job".
 *
 * The followings are the available columns in table 'tbl_job':
 * @property integer $id
 * @property integer $user_id
 * @property string $name
 * @property integer $status
 * @property integer $subjobtype_id
 * @property string $duration
 * @property integer $budget_id
 * @property string $jobdesc
 * @property string $fileattached
 * @property string $skillsrequired
 * @property integer $biddingperiod
 * @property integer $id_winner
 * @property integer $viewed
 *
 * The followings are the available model relations:
 * @property Bidjob[] $bidjobs
 * @property Comment[] $comments
 * @property Feedback[] $feedbacks
 * @property Users $user
 * @property SubJobtype $subjobtype
 * @property Budget $budget
 * @property Users $idWinner
 */
class Job extends CActiveRecord
{
	/**
	 * Returns the static model of the specified AR class.
	 * @param string $className active record class name.
	 * @return Job the static model class
	 */
     public $user;
    public $jobtype_id;
	
	public function getJobType() {
		$sql = Yii::app()->db->createCommand('select * from tbl_jobtype');
		$type = CHtml::listData($sql->queryAll(), 'id', 'name');
		return $type;
	}
	public function getBudgetId() {
		$sql = Yii::app()->db->createCommand('select * from tbl_budget');
		$type = CHtml::listData($sql->queryAll(), 'id', 'money');
		return $type;
	}
	public function getJob($id) {
		$connection=Yii::app()->db;
		$sql="SELECT name FROM tbl_sub_jobtype WHERE id = :id ";
		$command = $connection->createCommand($sql);
		$command->bindParam(":id",$id,PDO::PARAM_STR);
		$dataReader=$command->query();          
		$rows=$dataReader->readAll();
		$namevalue=array();
		foreach ($rows as $max) {
			$namevalue = $max['name'];
		}   
		return $namevalue; // which is the value u need
	}
	
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return 'tbl_job';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('user_id, name, status, subjobtype_id, duration, budget_id, jobdesc, skillsrequired, biddingperiod', 'required'),
			array('user_id, status, subjobtype_id, budget_id, biddingperiod, id_winner, viewed', 'numerical', 'integerOnly'=>true),
			array('name', 'length', 'max'=>200),
			array('duration', 'length', 'max'=>100),
			array('fileattached', 'length', 'max'=>50),
			array('skillsrequired', 'length', 'max'=>255),
			// The following rule is used by search().
			// Please remove those attributes that should not be searched.
			array('id, user_id, name, status, subjobtype_id, duration, budget_id, jobdesc, fileattached, skillsrequired, biddingperiod, id_winner, viewed', 'safe', 'on'=>'search'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
			'bidjobs' => array(self::HAS_MANY, 'Bidjob', 'job_id'),
			'comments' => array(self::HAS_MANY, 'Comment', 'job_id'),
			'feedbacks' => array(self::HAS_MANY, 'Feedback', 'job_id'),
			'user' => array(self::BELONGS_TO, 'Users', 'user_id'),
			'subjobtype' => array(self::BELONGS_TO, 'SubJobtype', 'subjobtype_id'),
			'budget' => array(self::BELONGS_TO, 'Budget', 'budget_id'),
			'idWinner' => array(self::BELONGS_TO, 'Users', 'id_winner'),
            'commentCount' => array(self::STAT, 'Comment', 'job_id'),
		);
	}
    public function attributeLabels()
	{
		return array(
				'name' => 'Tên công việc',
                'user_id' => 'Người đăng công việc',
				'subjobtype_id' => 'Lĩnh vực',
				'duration' => 'Khoảng thời gian hoàn thành',
				'budget_id' => 'Lương hoàn thành',
				'jobdesc' => 'Mô tả công việc',
                'fileattached' => 'File mô tả đính kèm',
				'skillsrequired' => 'Kĩ năng yêu cầu',
				'biddingperiod' => 'Thời gian đăng kí công việc',
	
		);
	}
    	public function getProjectStatus() {
		return array(
			2=>'Open',
			''=>'All',
			3=>'Canceled',
		);
	}
	public function getBidperiod() {
		return array(
			61=>'Any',
			1=>'1 day',
			3=>'3 days',
			7=>'1 week',
			14=>'2 weeks',
			21=>'3 weeks',
			30=>'1 month',
			60=>'2 months',
		);
	}
	/**
	 * @return array customized attribute labels (name=>label)
	 */
	protected function beforeValidate(){
		if ($this->isNewRecord) {
			$this->id_winner =1;
			$this->user_id = $this->user;
			$this->status = 1;
			$this->viewed = 0;
		}
		return parent::beforeValidate();
	}
	public function setUserid($u){
		$this->user = $u;
	}
    public function search($search)
	{
		// Warning: Please modify the following code to remove attributes that
		// should not be searched.

		$criteria=new CDbCriteria;

		$criteria->compare('name',$search,true);

		return new CActiveDataProvider($this, array(
			'criteria'=>$criteria,
            'pagination'=>array('pageSize'=>6),
		));
	}
	
	public function searchAdvance()
	{

		// Warning: Please modify the following code to remove attributes that
		// should not be searched.

		$criteria=new CDbCriteria;
		
		//$criteria->compare('name',$search,true);
		
		$criteria->compare('jobdesc',$this->jobdesc,true);
		
		$criteria->compare('subjobtype_id',$this->subjobtype_id);
		
		$criteria->compare('status',$this->status);
		
		//$criteria->compare('budget_id',$this->budget_id,true);	
		/*
		$criteria2 = new CDbCriteria(array(
                        'condition'=>'biddingperiod >= :start AND biddingperiod <= :end',
                        'params'=>array (	
							':start' => 1,
							':end' => $this->biddingperiod,
							),
					));	
		$criteria->mergeWith($criteria2);
		*/
		return new CActiveDataProvider($this, array(
			'criteria'=>$criteria,
            'pagination'=>array('pageSize'=>6),
		));
	}	
	
	public function addComment($comment) {
		$comment->job_id = $this->id;
		return $comment->save();
	}
}